package com.hanthink.system.service.impl.pc;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hanthink.common.exception.CustomException;
import com.hanthink.system.domain.pc.EmResourceCall;
import com.hanthink.system.domain.pc.EmResourceCallFault;
import com.hanthink.system.domain.pc.EmResourceCallGroup;
import com.hanthink.system.domain.pc.EmTask;
import com.hanthink.system.mapper.EmResourceCallFaultMapper;
import com.hanthink.system.mapper.EmResourceCallGroupMapper;
import com.hanthink.system.mapper.EmResourceCallMapper;
import com.hanthink.system.service.pc.IEmResourceCallService;

/**
 * 资源呼叫维护Service业务层处理
 * 
 * @author hanthink
 * @date 2021-01-25
 */
@Service
public class EmResourceCallServiceImpl implements IEmResourceCallService {
	@Autowired
	private EmResourceCallMapper emResourceCallMapper;

	@Autowired
	private EmResourceCallGroupMapper emResourceCallGroupMapper;

	@Autowired
	private EmResourceCallFaultMapper emResourceCallFaultMapper;

	/**
	 * 查询资源呼叫维护
	 * 
	 * @param id 资源呼叫维护ID
	 * @return 资源呼叫维护
	 */
	@Override
	public EmResourceCall selectEmResourceCallById(Long id) {
		return emResourceCallMapper.selectEmResourceCallById(id);
	}

	/**
	 * 查询资源呼叫维护列表
	 * 
	 * @param emResourceCall 资源呼叫维护
	 * @return 资源呼叫维护
	 */
	@Override
	public List<EmResourceCall> selectEmResourceCallList(EmResourceCall emResourceCall) {
		return emResourceCallMapper.selectEmResourceCallList(emResourceCall);
	}

	/**
	 * 新增资源呼叫维护
	 * 
	 * @param emResourceCall 资源呼叫维护
	 * @return 结果
	 */
	@Override
	public int insertEmResourceCall(EmResourceCall emResourceCall) {
		// 校验编码是否已存在
		EmResourceCall emResource = new EmResourceCall();
		emResource.setTypeCode(emResourceCall.getTypeCode());
		List<EmResourceCall> callList = emResourceCallMapper.selectEmResourceCallList(emResource);
		if (callList.size() > 0) {
			throw new CustomException("编码" + emResourceCall.getTypeCode() + "已存在，请核实");
		}
		return emResourceCallMapper.insertEmResourceCall(emResourceCall);
	}

	/**
	 * 修改资源呼叫维护
	 * 
	 * @param emResourceCall 资源呼叫维护
	 * @return 结果
	 */
	@Override
	public int updateEmResourceCall(EmResourceCall emResourceCall) {
		return emResourceCallMapper.updateEmResourceCall(emResourceCall);
	}

	/**
	 * 批量删除资源呼叫维护
	 * 
	 * @param ids 需要删除的资源呼叫维护ID
	 * @return 结果
	 */
	@Override
	public int deleteEmResourceCallByIds(Long[] ids) {
		for (Long id : ids) {
//    		EmResourceCall dictType = selectEmResourceCallById(id);
			EmResourceCallGroup callGroup = new EmResourceCallGroup();
			callGroup.setTypeKey(id);
			List<EmResourceCallGroup> groupList = emResourceCallGroupMapper.selectEmResourceCallGroupList(callGroup);
			if (groupList.size() > 0) {
				for (EmResourceCallGroup group : groupList) {
					emResourceCallGroupMapper.deleteEmResourceCallGroupById(group.getId());
				}
			}

			EmResourceCallFault callFault = new EmResourceCallFault();
			callFault.setTypeKey(id);
			List<EmResourceCallFault> callFaultList = emResourceCallFaultMapper
					.selectEmResourceCallFaultList(callFault);
			if (callFaultList.size() > 0) {
				for (EmResourceCallFault group : callFaultList) {
					emResourceCallFaultMapper.deleteEmResourceCallFaultById(group.getId());
				}
			}
//            if (emResourceCallGroupMapper.countCallListByCode(id) > 0 &&  emResourceCallGroupMapper.countCallListByCode(id) > 0 )
//            {
//                throw new CustomException(String.format("%1$s已存在子项不能删除", dictType.getTypeName()));
//            }
		}
		return emResourceCallMapper.deleteEmResourceCallByIds(ids);
	}

	/**
	 * 删除资源呼叫维护信息
	 * 
	 * @param id 资源呼叫维护ID
	 * @return 结果
	 */
	@Override
	public int deleteEmResourceCallById(Long id) {
		return emResourceCallMapper.deleteEmResourceCallById(id);
	}
}
